/**
 * yiqihao(一起好商务股份有限公司) copyright 
 */
package com.p2p.backend.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.p2p.base.annotation.PermissionNeed;
import com.p2p.base.model.BackendUser;
import com.p2p.base.model.Oplog;
import com.p2p.base.util.LoginUtil;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.dal.model.LoopNotice;
import com.p2p.front.service.LoopNoticeService;

/**
 *
 * @author leiruiqi@yiqihao.com
 *
 * 2015年2月15日 下午4:23:22
 */
@RequestMapping("/loopNotice")
@Controller
public class LoopNoticeController extends BaseController{

	/** Logger */
	private static final Logger logger = LoggerFactory
			.getLogger(LoopNoticeController.class);
	@Resource
	private LoopNoticeService loopNoticeService;
	
	@RequestMapping("/showAll")
	public void showAllNotice(HttpServletResponse response){
		List<LoopNotice> loopMoticeList = loopNoticeService.listAll();
		Map<String,Object> data = new HashMap<String,Object>();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("res", 1);
		jsonObject.put("data", data);
		
		data.put("rows", loopMoticeList);
		data.put("total", loopMoticeList.size());
		this.returnJsonObject(response, jsonObject);
		return;
	}
	
	@PermissionNeed("notice")
	@RequestMapping("/save")
	public void save(LoopNotice loopNotice,HttpServletResponse response,HttpServletRequest request){
		logger.info("save loopNotice info="+loopNotice.getInfo());
		BackendUser user = (BackendUser)LoginUtil.getCurrentUser();
		if(loopNotice.getId()!=null){
			loopNotice.setUpdateby(user.getId().toString());
			loopNoticeService.update(loopNotice);
		}else{
			loopNotice.setUpdateby(user.getId().toString());
			loopNotice.setCreateby(user.getId().toString());
			loopNoticeService.create(loopNotice);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("res", 1);
	}
	
	@RequestMapping("getById")
	public void getById(Long id,HttpServletResponse response){
		LoopNotice loopNotice = loopNoticeService.getById(id);
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("res", 1);
		jsonObject.put("data", loopNotice);
		
		this.returnJsonObject(response, jsonObject);
	}

}
